﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

using PowerCreator.LMS.Web.Project;
using PowerCreator.LMS.Project.Entity;
using PowerCreator.LMS.Course.Entity;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Web.Paper;
using PowerCreator.LMS.Paper.Entiry;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.DL.Project.Entity;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.Web.Catalog;

public partial class Theme_3_StudyProject : PowerCreator.LMS.Web.Base.BasePage
{
    protected PowerCreator.LMS.Web.Config.WebConfig config = new PowerCreator.LMS.Web.Config.WebConfig();
    PowerCreator.LMS.Web.DL.Project.TOCourseList objCourselist = new PowerCreator.LMS.Web.DL.Project.TOCourseList();
    PowerCreator.LMS.ViewQuery.ViewQuery ov = new PowerCreator.LMS.ViewQuery.ViewQuery();
    PowerCreator.LMS.Web.CMS.New opNews = new PowerCreator.LMS.Web.CMS.New();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "UserLogin.aspx";
    protected string classID = "0";
    protected string courseListID = "0";

    protected void Page_Load(object sender, EventArgs e)
    {
        CheckUser();
        BinderRPT();
        if (Request.QueryString["CourseListID"] != null)
        {
            courseListID = Request.QueryString["CourseListID"].ToString();
            ViewState["CourseListID"] = courseListID;
            classID = Request.QueryString["ClassID"].ToString();
            ViewState["ClassID"] = classID;
            GetThisProject(int.Parse(classID));
        }
    }


    private string GetNewSearch()
    {
        string sNewIDList = "0";
        string search = "";
        string rtsearch = "";

        DataTable dt = new DataTable();
        DataTable dt2 = new DataTable();
        string sql = "";
        search = "SELECT * FROM VM_Sys_ProjectUser where UserID=" + login.UserID;
        dt = ov.Query(search);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sql = "select NewID from TNew where  FKNewTrainclaIDList like '%," + dt.Rows[i]["ClassID"].ToString() + ",%'";
            dt2 = ov.Query(sql);
            for (int k = 0; k < dt2.Rows.Count; k++)
            {
                sNewIDList += "," + dt2.Rows[k]["NewID"].ToString();
            }
            dt2.Clear();
        }
        rtsearch = " NewState=1 and NewID in (" + sNewIDList + ")";
        return rtsearch;
    }

    private void BinderRPT()
    {
        DataTable dt = new DataTable();
        string orderby = "NewID desc";
        string search = GetNewSearch();
        int iPageSize = 3;
        dt = opNews.Query(search, orderby, iPageSize, 1);
        if (dt.Rows.Count == 0)
        {
            td_MyNew.Visible = false;
            br_MyNew.Visible = false;
            return;
        }
        rpt_New.DataSource = dt;
        rpt_New.DataBind();
    }

    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectURL(url);
        }

        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (login == null)
        {
            Script.RedirectURL(url);
        }

    }
    private void GetThisProject(int clid)
    {
        PowerCreator.LMS.DL.Project.Entity.VOTOCourseList vo= objCourselist.Load(clid);
        BinderRPTProjectResource(vo.CourseID);

        lbl_ProjectName2.Text = vo.CourseListName;
    }
    private void BinderRPTProjectResource(string courselist)
    {
        VOTOSchoolStage vostage = new VOTOSchoolStage();
        VOTCatalog vocatalog = new VOTCatalog();
        TOSchoolStage objstage = new TOSchoolStage();
        Catalog objcatalog = new Catalog();
        PowerCreator.LMS.ViewQuery.ViewQuery vo = new PowerCreator.LMS.ViewQuery.ViewQuery();
        string sql = string.Empty;
        int per = 0;
        int OtherPer = 100;
        DataTable dt01 = new DataTable();
        PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();

        string[] singleCourse = courselist.Split('#');
        string courseIDList = string.Empty; 

        if (singleCourse.Length > 0)
        {
            for (int i = 0; i < singleCourse.Length; i++)
            {
                string[] courseID = singleCourse[i].Split(',');
                if (courseID.Length == 4)
                {
                    if (courseIDList.Length > 0)
                        courseIDList += "," + courseID[0];
                    else
                        courseIDList = courseID[0];

                }
            }
        }
        if(courseIDList.Length==0)
            courseIDList="0";
        string search=" Course_Id in ("+courseIDList+")";
        PowerCreator.LMS.Web.Course.Course obj=new Course();
        DataTable dt=new DataTable();
        dt=obj.Query(search);
        dt.Columns.Add("ResourceImageUrl");
        dt.Columns.Add("strxueke");
        dt.Columns.Add("strxueduan");
        dt.Columns.Add("per");
        dt.Columns.Add("OtherPer");
        int a =0;
        int b =100;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            a = 0;
            b = 100;
            per = 0;
            OtherPer = 100;
            dt.Rows[i]["ResourceImageUrl"] = "<a href=\"OpenCourse.aspx?CourseID=" + dt.Rows[i]["Course_Id"] + "&ClassID=" + ViewState["ClassID"] + "\" title=\"" + dt.Rows[i]["CourseName"] + "\"><img alt=\"" + dt.Rows[i]["CourseName"] + "\"  src=\"" + dt.Rows[i]["CourseImgUrl"] + "\" class=\"imgstyle\" width=\"117\" height=\"87\" /></a>";
            vostage = objstage.Load(int.Parse(dt.Rows[i]["Xueduan"].ToString()));
            dt.Rows[i]["strxueduan"] = vostage.SchoolStage;
            vocatalog = objcatalog.Load(int.Parse(dt.Rows[i]["Xueke"].ToString()));
            dt.Rows[i]["strxueke"] = vocatalog.CatalogName;
            if (login != null)
            {
                sql = "Select sum(SscrlogTotalTime) as SscrlogTotalTime from TStudentStudyCouReslog where FKSscrlogUserID=" + login.UserID.ToString() + " and FKSscrlogCourseID=" + dt.Rows[i]["Course_Id"].ToString() + " and FKSscrlogTrainclaID=" + ViewState["ClassID"].ToString();
                dt01 = vo.Query(sql);
               
                if (dt01.Rows.Count == 1)
                {
                    if (singleCourse.Length > 0)
                    {
                        for (int j = 0; j < singleCourse.Length; j++)
                        {
                            string[] coursepar = singleCourse[i].Split(',');
                            if (coursepar.Length == 4)
                            {
                                if (dt.Rows[j]["Course_Id"].ToString() == coursepar[0])
                                {
                                    if (dt01.Rows[0]["SscrlogTotalTime"].ToString().Length > 0)
                                    {
                                        a = int.Parse(dt01.Rows[0]["SscrlogTotalTime"].ToString())*100;
                                        b = int.Parse(coursepar[2])*3600;
                                        per = Convert.ToInt32(a / b);
                                        OtherPer = 100 - per;
                                    }
                                    
                               
                               
                                }


                            }
                        }
                    }
                }
            }
            dt.Rows[i]["per"] = per;
            dt.Rows[i]["OtherPer"] = OtherPer;

        }

        rpt_ProjectResource.DataSource = dt;
        rpt_ProjectResource.DataBind();


    }

    private string GetPicName(string fileName,int Resourceclass)
    {
        string rt = "images/Exam.jpg";
        if (Resourceclass == 1)
        {
            rt = "000.jpg";
        }
        else if(Resourceclass == 2)
        {
            string[] fileNames = fileName.Split('.');
            if (fileNames.Length == 2)
                rt = fileNames[0].ToString() + ".jpg";
        }
        return rt;
    }
    private string GetPath(string Path, int Resourceclass)
    {
        string rt = Path;
        if (Resourceclass != 1 && Resourceclass != 2)
        {
            rt = "";
        }
        return rt;
    }

}